from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
import numpy as np

x_data = [[1, 2],
          [2, 3],
          [3, 1],
          [4, 3],
          [5, 3],
          [6, 2]]
y_data = [[0],
          [0],
          [0],
          [1],
          [1],
          [1]]

#构建模型序列对象
model = Sequential()
#构建dense层，单元个数=1,输入维度input_dim=2,激活函数activation=sigmoid
model.add(Dense(1, input_dim=2, activation='sigmoid'))

#配置模型：损失loss=binary_crossentropy二进制(二分类)交叉熵，优化器optimizer=sgd随机梯度下降
sgd = SGD(lr=0.1) # 学习率lr=0.1
model.compile(loss='binary_crossentropy', optimizer=sgd)
#打印模型结构
model.summary()
#训练模型：epoch=2000
model.fit(x_data, y_data, epochs=2000)
#预测结果:predict_classes指的是预测的类别；predict预测值(sigmoid输出的0-1之间的数)
print("2,1", model.predict_classes(np.array([[2, 1]])))
print("6,5", model.predict_classes(np.array([[6, 5]])))